﻿Imports System.Collections.Specialized

Imports System.IO
Imports System.Globalization
Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Collections.ObjectModel
Imports System
Imports System.ComponentModel
Imports System.Xml.Serialization
Imports System.Threading
Imports Hinshelwood.TFSHeatITSM.TeamFoundationExtensions
Imports QuantumBitDesigns.Core
Imports Infragistics.Windows.Ribbon
Imports Hinshelwood.TFSHeatITSM.Controlers.Data.Entities

Namespace Controlers

    Public Class QueryControler(Of TView As {UIElement, ITeamSystemView(Of TView), IHeatView(Of TView)})
        Inherits ControlerBase(Of TView)



        Public Sub New(ByVal View As TView)
            MyBase.New(View)
        End Sub

        Protected Overrides Sub Initilise()

        End Sub

        Protected Overrides Sub InitiliseObjects()
            m_CallsNotInTFS = New ObservableList(Of CallLog)(View.Dispatcher)

        End Sub

        Protected Overrides Sub InitiliseLoad()
            System.Threading.ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf LoadCallsNotInTFS_Async))
        End Sub

        Protected Overrides Sub InitiliseNavigation()

        End Sub

        Protected Overrides Sub DisposeManagedObjects()
            m_CallsNotInTFS = Nothing
        End Sub

        Protected Overrides Sub DisposeUnmanagedObjects()

        End Sub

        Public Overrides Sub Reset()
           InitiliseLoad
        End Sub

        Public ReadOnly Property IsDependancysReady() As Boolean
            Get
                Return View.TeamSystemControler.State = ControlerStates.Ready And View.HeatControler.State = ControlerStates.Ready
            End Get
        End Property

        Public ReadOnly Property AvailableCallLogs() As ObservableList(Of CallLog)
            Get
                Return View.HeatControler.m_AvailableCallLogs
            End Get
        End Property

        Public ReadOnly Property AvailableWorkItems() As ObservableList(Of WorkItem)
            Get
                Return View.TeamSystemControler.m_AvailableWorkItems
            End Get
        End Property


#Region " Query: Calls not in TFS "

        Private m_CallsNotInTFS As ObservableList(Of CallLog)
        Private m_CallNotInTFSSelected As CallLog

        Public Property CallNotInTFSSelected() As CallLog
            Get
                Return m_CallNotInTFSSelected
            End Get
            Set(ByVal value As CallLog)
                If Not m_CallNotInTFSSelected Is value Then
                    m_CallNotInTFSSelected = value
                    OnPropertyChanged("CallNotInTFSSelected")

                End If
            End Set
        End Property

        Public ReadOnly Property CallsNotInTFS() As ObservableCollection(Of CallLog)
            Get
                Return m_CallsNotInTFS.ObservableCollection
            End Get
        End Property

        Public Sub LoadCallsNotInTFS_Async()
            While Not IsDependancysReady
                System.Threading.Thread.Sleep(100)
            End While
            '---------------------
            m_CallsNotInTFS.Clear()

            Dim wiLookup As ILookup(Of Integer?, WorkItem) = AvailableWorkItems.ToLookup(Function(p) p.GetHeatItsmId, Function(p) p)

            Dim outer = _
                 From c In AvailableCallLogs _
                 From p In wiLookup(c.CallID).DefaultIfEmpty() _
                 Where p Is Nothing _
                 Select _
                  c

            For Each i In outer
                m_CallsNotInTFS.Add(i)
            Next

        End Sub

#End Region



        Protected Overrides Sub RecalculateState()

        End Sub

        Protected Overrides Sub PropertyHasChanged(ByVal PropertyName As String)

        End Sub


    End Class

End Namespace